package com.dotools.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DevicesUtils_MeiZu {

    private static Boolean isMeizu = false;

    /**
     * 是否为魅族手机
     *
     * @return
     */
    public static boolean isMeizuDevice() {
        isMeizu = Build.BRAND.equalsIgnoreCase("Meizu");
        return isMeizu;
    }

    /**
     * 魅族是否需要开启悬浮窗
     *
     * @return
     */
    public static boolean needFloatWindowPermission() {
        return DevicesUtils_MeiZu.isMeizuDevice() && Build.VERSION.SDK_INT >= 21;
    }

    /**
     * 魅族是否拥有悬浮窗权限
     *
     * @param context
     * @return
     */
    public static boolean hasFolatWindowPermission(Context context) {
        if (needFloatWindowPermission()) {
            return checkOp(context, OP_SYSTEM_FLOAT_WINDOW) == 0;
        }
        return false;
    }

    /**
     * 系统权限管理中心服务
     */
    public static final String APP_OPS_SERVICE = "appops";

    /**
     * 系统悬浮窗权限
     */
    public static final int OP_SYSTEM_FLOAT_WINDOW = 24;

    public static int checkNofication(Context context) {
        return checkOp(context, 25);
    }

    @SuppressWarnings({
            "unchecked", "rawtypes"
    })
    public static int checkOp(Context context, int op) {
        final int version = Build.VERSION.SDK_INT;
        if (version >= 19) {
            Object object = context.getSystemService(APP_OPS_SERVICE);
            Class c = object.getClass();
            try {
                Class[] cArg = new Class[3];
                cArg[0] = int.class;
                cArg[1] = int.class;
                cArg[2] = String.class;
                Method lMethod = c.getDeclaredMethod("checkOp", cArg);
                return (Integer) lMethod.invoke(object, op, Binder.getCallingUid(),
                        context.getPackageName());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

    public static void openMeiZuAutoSetting(Context context) {
        Intent intent = new Intent();
        ComponentName  componentName =  ComponentName.unflattenFromString("com.meizu.safe/.permission.SmartBGActivity");
        intent.setComponent(componentName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }
    public static void openMeiZuWindowActivity(Context context) {
        Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
        intent.setClassName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity");
        intent.putExtra("packageName", context.getPackageName());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /****
     * 跳转魅族市场详细APP页面
     * @param act   必须是ACTIVITY
     * @param pkg  APP包名
     */
    public static void jump2MeizuMarket(Activity act, String pkg){
        Intent intent= new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("mstore://details?package_name="+pkg));
        act.startActivity(intent);
    }
}
